import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import CustomDialog from "@/components/stationProductionPage/HntManage/pouringOrder/WBSHuitanGangbao/index.vue";
import CustomDialog1 from "@/components/PhysicalQualityPage/reboundStrengthManage/reboundEntrustList/index.vue";//委托单列表页

import {
    getConcreteCheckTaskByFid, //详情
    saveConcreteCheckTask, //保存
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
    getMangEquipmentPageList, //主要仪器设备名称及编号
    getBaseDataitemDetailByItemCode, //设计强度等级(Qdlevel)
    // getCheckTypeByUserList,//韩磊商量封掉暂时无用--cnn
    getUserCompanyTreeList,//机构树
    getBaseSgCompanyList,//机构树---只查施工单位
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            // WBS
            WBSShow: false,
            // WBS
            loading: false,
            title: "",
            isLock: false, //保存按钮是否可编辑
            dialogHeight: null,
            angleData: [
                //   {
                //   value: '-1',
                //   label: '0°'
                // },
                {
                    value: "0",
                    label: "90°",
                },
                {
                    value: "1",
                    label: "60°",
                },
                {
                    value: "2",
                    label: "45°",
                },
                {
                    value: "3",
                    label: "30°",
                },
                {
                    value: "4",
                    label: "-30°",
                },
                {
                    value: "5",
                    label: "-45°",
                },
                {
                    value: "6",
                    label: "-60°",
                },
                {
                    value: "7",
                    label: "-90°",
                },
            ], //角度数据
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fLeadercompanyfullname: "",
                fLeadercompanyname: "",
                fWitnessid: "", //委托单id
                fWitnessno: "",//委托单编号
                fCompanyid: "", //施工单位id 隐藏
                fCompanyname: "", //施工单位名称 隐藏
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目名称 隐藏   工程名称
                fProjecttypeid: "", //工程部位id
                fProjecttypename: "", //工程部位
                fPlandates: this.common.day(0), //计划检测日期
                fTesttype: "", //试验条件
                fTestnote: "", //检测依据
                fJudgetype: "", //判定依据
                fEquipmentname: "", //主要仪器设备及编号
                fEquipmentid: "", //主要仪器设备及编号id
                fTestnote1: [], //检测依据
                fJudgetype1: [], //判定依据
                fEquipmentname1: [], //主要仪器设备及编号
                fSpecimenage: "", //龄期
                fConcretelevel: "", //设计强度等级(MPa)
                fConcretetype: "", //是否泵送
                fDirection: "", //回弹测试方向*
                fSurface: "", //测面状态
                fAngle: "", //测试角度
                fCreateuserid: "", //编制人id
                fCreateusername: "", //编制人
                fNature: "",//当前用户机构性质
                // isSubmit: "0", //0 保存 1提交
            },
            formObj: formJs.formObj, //报验信息

            btnShow: true,
            currentDetail: null,
            timeYMD: null, //当前日期
            timeYmdHms: null,
            selected: [], //多选接值


            // 委托单列表页
            isShow: false,
            // 委托单列表页

        };
    },
    components: {
        publicForm,
        CustomDialog,
        CustomDialog1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },

        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit();
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    var that = this
                    this.currentDetail = newVal;
                    if (this.statusNum != 1) {
                        setTimeout(() => {
                            that.getDetail(this.currentDetail) // currentRow

                        }, 200)
                    }
                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();

    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
        this.getCheckitemsList("3"); //回弹试验依据
        this.getCheckitemsList("4"); //回弹判定依据
        this.getEquipment() //主要仪器设备名称及编号

        this.btnShow = true;
        this.formObj.map(item => {
            item.disabled = false;
        });
        this.formObj[0].disabled = true;

        if (this.statusNum == 1) {
            if (this.userInfo.companyNature == "施工单位") {
                this.formObj[1].disabled = true;
                
                this.formData.fCompanyname = this.userInfo.companyFullName;
                this.formData.fCompanyid = this.userInfo.companyId;
            } else if (this.userInfo.companyNature == "总监办") {
                this.formObj[1].disabled = false;
                this.getTree();
            } else if (this.userInfo.companyNature == "中心试验室") {
                this.formObj[0].disabled = false;
                this.formObj[1].disabled = false;
                this.getBaseTree();
            }

            this.formObj[2].disabled = true;
            this.formObj[15].disabled = true;
            // this.getCheckTypeByUserList(this.userInfo.companyId);

            this.formData.fCreateuserid = this.userInfo.userId; // //编制人id
            this.formData.fCreateusername = this.userInfo.realName; // //编制人

            this.formData.fProjectid = this.userInfo.projectId; // 当前用户项目ID
            this.formData.fProjectname = this.userInfo.projectName; // 当前用户项目名称
            this.formObj[3].inputButton = true;
        } else if (this.statusNum == 3) {
            if (this.userInfo.companyNature == "施工单位") {
                this.formObj[1].disabled = true;
                this.formData.fCompanyname = this.userInfo.companyFullName;
                this.formData.fCompanyid = this.userInfo.companyId;
            } else if (this.userInfo.companyNature == "总监办") {
                this.formObj[1].disabled = false;
                this.getTree();
            } else if (this.userInfo.companyNature == "中心试验室") {
                this.formObj[0].disabled = false;
                this.formObj[1].disabled = false;
                this.getBaseTree();
            }
            this.formObj[2].disabled = true;
            this.formObj[15].disabled = true;
            this.formObj[3].inputButton = true;

        } else if (this.statusNum == 7) {
            this.formObj = formJs.formObj
            this.btnShow = false;
            this.formObj.map(item => {
                item.disabled = true;
            })
            this.formObj[3].inputButton = false;
        }
    },
    methods: {
        refreshList() { },
        getTree() {
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }
            this.formObj[1].options = [];
            getUserCompanyTreeList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        var info = this.commonJs.tranListToTreeData(companyTree)[0].children;
                        info.map(item => {
                            this.formObj[1].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                        this.formData.fCompanyname = this.formObj[1].options[0].label;
                        this.formData.fCompanyid = this.formObj[1].options[0].value;
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        //中心试验室---查施工单位
        getBaseTree() {
            var data = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId
            }
            this.formObj[1].options = [];
            getBaseSgCompanyList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var data = res.data.userCompanyTreeList;
                        data.map(item => {
                            this.formObj[1].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        })
                        this.formData.fCompanyname = this.formObj[1].options[0].label;
                        this.formData.fCompanyid = this.formObj[1].options[0].value;
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },

        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        // WBS树展示
        clickDialog(index, prop) {
            // console.log("clickDialog", index, prop);
            if (prop == "fProjecttypename") {
                if (this.formData.fCompanyname != "" && this.formData.fCompanyname != null) {
                    this.WBSShow = true;
                } else {
                    this.$message.warning("请先选择施工单位");
                }
            }
            return index, prop;
        },
        getWBS(data) { //获取选择WBS信息
            this.formData.fProjecttypename = data.fLongname //工程部位
            this.formData.fProjecttypeid = data.fNodeid //工程部位id
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        closeDialog() {
            this.$emit("close");
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 数据字典
        getBaseDataitemDetailByItemCode(status) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {
                this.formObj[10].options = [];
                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                this.formObj[10].options = arr
            })
        },

        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                modelType: num,
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        if (num == 3) { //回弹试验依据
                            this.formObj[6].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[6].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })
                        } else if (num == 4) { //回弹判定依据
                            this.formObj[7].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[7].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId
                                })
                            })
                        }
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 主要仪器设备名称及编号
        getEquipment() {
            getMangEquipmentPageList({
                // fUserid: this.userInfo.userId,
                fCompanyid: this.userInfo.companyId, //当前登录用户机构id
                keyWord: "", //设备名称模糊查询字段
                keyType: "混凝土回弹检测仪器设备", //设备类型
                fNote: "1",
                modelType: "2", // 0代办 1已办
                sublevel: 0, //查看下级  1是0否
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.formObj[8].options = [];
                        res.data.mangEquipmentList.list.map(item => {
                            this.formObj[8].options.push({
                                label: item.fEqptname + "/" + item.fEqptno,
                                value: item.fId
                            })
                        })
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 详情
        getDetail(row) {
            this.loading = true;
            getConcreteCheckTaskByFid({
                fId: row.fId
            }).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    this.formData = res.data.lrCheckConcreteTask;
                    if (this.statusNum != 7) {
                        if (row.fStatus == '被撤回') {
                            this.formObj[4].disabled = true;
                        } else {
                            this.formObj[4].disabled = false;
                        }
                    } else {
                        this.formObj[4].disabled = true;
                    }

                    //    检测依据
                    if (res.data.lrCheckConcreteTask.fTestnote != '') {
                        var fTestnote = []
                        this.formObj[6].options.map(item => {
                            res.data.lrCheckConcreteTask.fTestnote.split(",").map(i => {
                                if (item.label == i) {
                                    fTestnote.push(item)
                                }
                            })
                        })

                    } else {
                        fTestnote = []
                    }
                    //    检测依据
                    // 判定依据
                    if (res.data.lrCheckConcreteTask.fJudgetype != '') {
                        var fJudgetype = []
                        this.formObj[7].options.map(item => {
                            res.data.lrCheckConcreteTask.fJudgetype.split(",").map(i => {
                                if (item.label == i) {
                                    fJudgetype.push(item)
                                }
                            })
                        })

                    } else {
                        fJudgetype = []
                    }
                    // 判定依据
                    //    设备
                    if (res.data.lrCheckConcreteTask.fEquipmentid != '') {
                        var id = res.data.lrCheckConcreteTask.fEquipmentid.split(",")
                        var name = res.data.lrCheckConcreteTask.fEquipmentname.split(",")
                        var fEquipmentname = []
                        id.map((item, index) => {
                            fEquipmentname.push({
                                label: name[index],
                                value: item
                            })
                        })
                    } else {
                        fEquipmentname = []
                    }

                    //    设备
                    this.selected = [
                        {
                            name: "fTestnote1",
                            selected: fTestnote,
                        },
                        {
                            name: "fJudgetype1",
                            selected: fJudgetype,
                        },
                        {
                            name: "fEquipmentname1",
                            selected: fEquipmentname,
                        },
                    ];

                } else {
                    this.loading = false;
                    this.$message.error(res.msg);
                }
            });
        },
        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            if (b == 'fTestnote1') {
                var arr = []
                data.map(item => {
                    if (item.label) {
                        arr.push(item.label)
                    } else {
                        arr.push(item)
                    }
                })
                this.formData.fTestnote = arr.join(",");
            } else if (b == 'fJudgetype1') {
                var arr1 = []
                data.map(item => {
                    if (item.label) {
                        arr1.push(item.label)
                    } else {
                        arr1.push(item)
                    }

                })
                this.formData.fJudgetype = arr1.join(",")
                // this.formData.fJudgetype1 = arr1
            } else if (b == 'fDirection') {
                if (data == '水平方向') {
                    this.formData.fSurface = "侧面";
                    this.formData.fAngle = "0°"
                    this.formObj[13].options = [{
                        label: "侧面",
                        value: "侧面"
                    }]
                    this.formObj[14].options = [{
                        label: "0°",
                        value: "0°"
                    }]
                } else if (data == '非水平方向') {
                    this.formData.fSurface = "表面";
                    this.formData.fAngle = "90°"
                    this.formObj[13].options = [{
                        label: "表面",
                        value: "表面"
                    }, {
                        label: "底面",
                        value: "底面"
                    }]
                    this.formObj[14].options = this.angleData
                }
            } else if (b == 'fCompanyname') {
                this.formData.fCompanyname = data.label;
                this.formData.fCompanyid = data.value;
                this.formData.fProjecttypename = "";
                this.formData.fProjecttypeid = "";
            }

        },
        inputFocus(index, prop) {
            // console.log("inputFocus", index, prop);
            //委托单列表选择
            if (prop == "fWitnessno") {
                this.isShow = true;
            }
            return index, prop;
        },

        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
            this.formData.userId = this.userInfo.userId; // 当前登录人id
            this.formData.userName = this.userInfo.realName; // 当前登录人姓名
            this.formData.fNature = this.userInfo.companyNature;//当前用户的机构性质
            // 设备
            var equipmentname = [];
            var equipmentnameid = [];
            this.formData.fEquipmentname1.map(item => {
                equipmentname.push(item.label);
                equipmentnameid.push(item.value)
            })
            this.formData.fEquipmentid = equipmentnameid.join(",")
            this.formData.fEquipmentname = equipmentname.join(",")
            // 设备
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            saveConcreteCheckTask(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false
                        this.$message.error(res.msg);
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },

        // 获取委托单列表数据
        closeDialog1() {
            this.isShow = false;
        },
        getEntrustRow(data) {
            // console.log("获取委托单列表数据", data);
            this.formData.fCompanyid = data.fCompanyid;//施工单位id
            this.formData.fCompanyname = data.fCompanyname;//施工单位名称
            this.formData.fProjectname = data.fProjectuse;//工程名称
            this.formData.fProjecttypeid = data.fProjecttypeid;//工程部位id
            this.formData.fProjecttypename = data.fProjecttypename;//工程部位
            this.formData.fSpecimenage = data.fSpecimenage;//龄期
            this.formData.fConcretelevel = data.fConcretelevel;//设计强度等级(MPa)
            this.formData.fConcretetype = data.fConcretetype;//是否泵送
            this.formData.fWitnessno = data.fEntrustno;//委托单编号
            this.formData.fWitnessid = data.fId;//委托单id
            this.formData.fTestnote = data.fTestnote;//检测依据
            this.formData.fJudgetype = data.fJudgetype;//判定依据
            // 检测依据
            if (data.fTestnote != '') {
                var fTestnote = []
                this.formObj[6].options.map(item => {
                    data.fTestnote.split(",").map(i => {
                        if (item.label == i) {
                            fTestnote.push(item)
                        }
                    })
                })

            } else {
                fTestnote = []
            }
            // 检测依据
            // 判定依据
            if (data.fJudgetype != '') {
                var fJudgetype = []
                this.formObj[7].options.map(item => {
                    data.fJudgetype.split(",").map(i => {
                        if (item.label == i) {
                            fJudgetype.push(item)
                        }
                    })
                })

            } else {
                fJudgetype = []
            }
            // 判定依据
            this.selected = [
                {
                    name: "fTestnote1",
                    selected: fTestnote,
                },
                {
                    name: "fJudgetype1",
                    selected: fJudgetype,
                }
            ];
        },
        // 获取委托单列表数据

    },
};